package controller;

import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.ElementoBase;
import model.Escursione;
import model.Hotel;
import model.PacchettoPersonale;
import model.PacchettoPredefinito;
import model.Utente;
import model.Volo;
import util.Converter;
import dto.ElementoBaseDTO;
import dto.EscursioneDTO;
import dto.HotelDTO;
import dto.PacchettoPersonaleDTO;
import dto.PacchettoPredefinitoDTO;
import dto.UtenteDTO;
import dto.VoloDTO;

/**
 * Session Bean implementation class Ricerca
 */
@Stateless
public class Ricerca implements RicercaInterface {
	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
    /**
     * Default constructor. 
     */
    public Ricerca() {
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public List<ElementoBaseDTO> getElementiBase() {
	List<ElementoBase> plist= (List<ElementoBase>) em.createQuery(
		        "SELECT e FROM ElementoBase e")
		        .getResultList();
	return Converter.convertElementiToDTO(plist);
	}

	@Override
	public List<HotelDTO> getListHotel() {
	List<Hotel> plist= (List<Hotel>) em.createQuery(
		        "SELECT e FROM Hotel e")
		        .getResultList();
	return Converter.convertHotelToDTO(plist);
	}
	
	@Override
	public List<VoloDTO> getListVolo() {
	List<Volo> plist= (List<Volo>) em.createQuery(
		        "SELECT e FROM Volo e")
		        .getResultList();
	return Converter.convertVoliToDTO(plist);
	}
	
	@Override
	public List<EscursioneDTO> getListEscursione() {
	List<Escursione> plist= (List<Escursione>) em.createQuery(
		        "SELECT e FROM Escursione e")
		        .getResultList();
	return Converter.convertEscursioniToDTO(plist);
	}
	@Override
	public List<PacchettoPredefinitoDTO> getPacchettiPredefiniti() {
		List<PacchettoPredefinito> plist= (List<PacchettoPredefinito>) em.createQuery(
		        "SELECT e FROM PacchettoPredefinito e where e.approvato=1")
		        .getResultList();
	return Converter.convertPredToDTO(plist);
	}

	@Override
	public List<UtenteDTO> getUtenti() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public VoloDTO getDettaglioVolo(ElementoBaseDTO e) {
	if(e!=null){
	Volo v = em.find(Volo.class, e.getIdElementoBase());
	if(v!=null)
	return Converter.convertToDTO(v);
	else return null;
	}
	else return null;
	}

	@Override
	public EscursioneDTO getDettaglioEscursione(ElementoBaseDTO e) {
		if(e!=null){
		Escursione esc = em.find(Escursione.class, e.getIdElementoBase());
		if(esc!=null)
		return Converter.convertToDTO(esc);
		else return null;
		}
		else return null;
	}

	@Override
	public HotelDTO getDettaglioHotel(ElementoBaseDTO e) {
		if(e!=null){
		Hotel h = em.find(Hotel.class, e.getIdElementoBase());
		if(h!=null) 
		return Converter.convertToDTO(h);
		else return null;
		}
		else return null;
	}

	@Override
	public ElementoBaseDTO getElementoPK(int pk) {
		ElementoBase el = em.find(ElementoBase.class, pk);
		if(el!=null)
		return Converter.convertToDTO(el);
		else return null;
	}

	@Override
	public PacchettoPersonaleDTO getPacchettoPersonalePK(int pk) {
		
		Utente actual = em.find(Utente.class,context.getCallerPrincipal().getName());
		PacchettoPersonale p = em.find(PacchettoPersonale.class, pk);
		if(p==null) return null;
		boolean ok = false;
		for(PacchettoPersonale pack : actual.getPacchettiPersonali()){
			if(pack.getIdPacchettoPersonale()==pk)
				ok=true;
		}
		if(ok)
			return Converter.convertToDTO(p);
		else return null;
	}

	@Override
	public PacchettoPersonaleDTO getPacchettoPersonaleCode(String code) {
		if (code.length()<16) return null;
		int size = code.length()-15;
		String pks = "";
		for(int i=0;i<size;i++){
			pks+=code.charAt(i);
		}
		String cs="";
		for(int i=size;i<code.length();i++){
			cs+=code.charAt(i);
		}
		int pk=0;

		try{
		pk = Integer.parseInt(pks);
		}catch(Exception e){
			return null;
		}

		PacchettoPersonale p = em.find(PacchettoPersonale.class, pk);
		if(p==null)
			return null;

		if(p.getRandomcode().equals(cs))
			return Converter.convertToDTO(p);
		else
			return null;
		
	}


}
